package sn.gandal.mobile.kalpet.android.guichet;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

/**
 * Created by IntelliJ IDEA.
 * User: SOWDIOMYERO
 * Date: 1 nov. 2012
 * Time: 13:10:23
 * Version : ${VERSION}
 * To change this template use File | Settings | File Templates.
 */


/**
 * Utility class for dealing with persistence.
 *
 * @author sowdiomyero
 */
public class PersistenceService {
    private static String DEFAULT_PU = "guichet";

    private static ThreadLocal<PersistenceService> instance = new ThreadLocal<PersistenceService>() {
        @Override
        protected PersistenceService initialValue() {
            return new PersistenceService();
        }
    };
    @PersistenceContext(name = "guichet")
    private EntityManager em;

    private EntityManagerFactory emf;


    private UserTransaction utx;

    private PersistenceService() {
        try {
            this.emf = (EntityManagerFactory) new InitialContext().lookup("java:comp/env/persistence/" + DEFAULT_PU);
            em=emf.createEntityManager();
        } catch (NamingException ex) {
            throw new RuntimeException(ex);
        }
        try {
           this.utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
        } catch (NamingException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * Returns an instance of PersistenceService.
     *
     * @return an instance of PersistenceService
     */
    public static PersistenceService getInstance() {
        return instance.get();
    }


    private static void removeInstance() {
        instance.remove();
    }

    /**
     * Returns an instance of EntityManager.
     *
     * @return an instance of EntityManager
     */
    public EntityManager getEntityManager() {
        return em;
    }

    /**
     * Begins a resource transaction.
     */
    public void beginTx() {
        try {
            utx.begin();
            em.joinTransaction();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * Commits a resource transaction.
     */
    public void commitTx() {
        try {
            utx.commit();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * Rolls back a resource transaction.
     */
    public void rollbackTx() {
        try {
            utx.rollback();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * Closes this instance.
     */
    public void close() {
        removeInstance();
    }

    private EntityManager lookupEntityManager(String DEFAULT_PU) {
        try {
           this.em = (EntityManager) new InitialContext().lookup("java:comp/env/persistence/" + DEFAULT_PU);
           this.utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
       } catch (NamingException ex) {
           throw new RuntimeException(ex);
       }
        return em;
    }

}